class EventBus {
  constructor() {
    this.eventMap = {};
  }

  on(eventName, fn) {
    let eventFuns = this.eventMap[eventName];
    if (!eventFuns) {
      this.eventMap[eventName] = [];
    }
    this.eventMap[eventName].push(fn);
  }

  off(eventName, fn) {
    let eventFuns = this.eventMap[eventName];
    if (!eventFuns) {
      throw new Error(`没有${eventName}事件！`);
    }
    for (let i = 0; i < eventFuns.length; i++) {
      const ele = eventFuns[i];
      if (ele === fn) {
        eventFuns.splice(i, 1);
        break;
      }
    }
    if (eventFuns.length === 0) {
      delete this.eventMap[eventName];
    }
  }

  emit(eventName, ...args) {
    let eventFuns = this.eventMap[eventName];
    if (!eventFuns) {
      throw new Error(`没有${eventName}事件！`);
    }
    for (const ele of object) {
      ele(...args);
    }
  }
}

class EventBus2 {
  constructor() {
    this.eventMap = {};
  }

  on(eventName, fn) {
    if (!this.eventMap[eventName]) {
      this.eventMap[eventName] = [];
    }
    this.eventMap[eventName].push(fn);
  }

  off(eventName, fn) {
    if (!this.eventMap[eventName]) {
      throw new Error(`${eventName}事件不存在！`);
    }
    for (let i = 0; i < this.eventMap[eventName].length; i++) {
      if (this.eventMap[eventName][i] === fn) {
        this.eventMap[eventName].splice(i, 1);
        break;
      }
    }

    if (this.eventMap[eventName].length === 0) {
      delete this.eventMap[eventName];
    }
  }

  emit(eventName, ...args) {
    if (!this.eventMap[eventName]) {
      throw new Error(`${eventName}事件不存在！`);
    }

    this.eventMap[eventName].forEach((ele) => {
      ele(...args);
    });
  }
}

class EventBus {
  constructor() {
    this.eventMap = {};
  }

  on(eventName, fn) {
    const events = this.eventMap[eventName];
    if (!events) {
      this.eventMap[eventName] = [];
    }
    this.eventMap[eventName].push(fn);
  }

  off(eventName, fn) {
    const events = this.eventMap[eventName];
    if (!events) {
      throw new Error(`没有${eventName}事件`);
    }
    for (let i = 0; i < events.length; i++) {
      if (events[i] === fn) {
        events.splice(i, 1);
        break;
      }
    }
    if (!events.length) {
      delete this.eventMap[eventName];
    }
  }

  emit(eventName, ...args) {
    const events = this.eventMap[eventName];
    if (!events) {
      throw new Error(`没有${eventName}事件`);
    }
    for (const fn of events) {
      fn(...args);
    }
  }
}

class EventBus {
  constructor() {
    this.eventMap = {};
  }

  on(eventName, fn) {
    const eventFuns = this.eventMap[eventName];
    if (!eventFuns) {
      this.eventMap[eventName] = [];
    }
    this.eventMap[eventName].push(fn);
  }

  off(eventName, fn) {
    const eventFuns = this.eventMap[eventName];
    if (!eventFuns) {
      throw new Error(`不存在${eventName}的事件`);
    }
    for (let i = 0; i < eventFuns.length; i++) {
      if (eventFuns[i] === fn) {
        eventFuns.splice(i, 1);
        break;
      }
    }
    if (!eventFuns.length) {
      delete this.eventMap[eventName];
    }
  }

  emit(eventName, ...args) {
    const eventFuns = this.eventMap[eventName];
    if (!eventFuns) {
      throw new Error(`不存在${eventName}的事件`);
    }
    for (const fn of eventFuns) {
      fn(...args);
    }
  }
}
